package xxm.utility;

import xxm.exception.UtilityException;

/**
 * 
 * @author xxm
 *
 */
public class BasicPathHelp {

	/**
	 * get application path
	 * @return
	 * @throws UtilityException
	 */
	public static String os_get_application_path() throws UtilityException {
		
		try {
			String strCurrentPath = BasicProperties.readValue("log_path");
			
			if( strCurrentPath == null || strCurrentPath.trim().isEmpty() ) {
				strCurrentPath = BasicUtility.getSystemEnv("APPDATA", "\\yatony\\");
				if( !BasicUtility.checkDirectory(strCurrentPath) ) {
					strCurrentPath = "";
				}
			}

			if( strCurrentPath == null || strCurrentPath.trim().isEmpty() ) {
				strCurrentPath = System.getProperty("user.dir")+"\\";
			}
			return strCurrentPath;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new UtilityException("BasicPathHelp getFileLen", e );
		}
	}
	
	/**
	 * get file name and suffix name
	 * @param fullName
	 * @return
	 * @throws UtilityException
	 */
	public static String _basic_get_name(final String fullName) throws UtilityException {
		
		String strTemp = fullName;
		
		int pos = fullName.lastIndexOf("\\");
		int pos2 = fullName.lastIndexOf("/");
		pos = Math.max(pos, pos2);
		
		if( pos != -1 ) {
			strTemp = fullName.substring(pos+1, fullName.length());
			return strTemp;
		}else {
			return "";
		}
		
	}
	
	/**
	 * get file dir
	 * @param fullName
	 * @return
	 * @throws UtilityException
	 */
	public static String _basic_get_file_dir(final String fullName) throws UtilityException {
		String strTemp = fullName;
		
		int pos = fullName.lastIndexOf("\\");
		int pos2 = fullName.lastIndexOf("/");
		
		pos = Math.max(pos, pos2);
		
		if( pos != -1 ) {
			strTemp = fullName.substring(0, pos);
			return strTemp;
		}else {
			return "";
		}
	
	}
	
	/**
	 * get file name
	 * @param fullName
	 * @return
	 * @throws UtilityException
	 */
	public static String _basic_get_noext_name( final String fullName) throws UtilityException {
		String strName = _basic_get_name(fullName);
		int pos = strName.lastIndexOf(".");
		if (pos != -1 ) {
			strName = strName.substring(0, pos);
			return strName;
		}
		return strName;
	}
	
}
